// component/editor.js
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    editorId: {
      type: String
    },
    fileName:{
      type: String
    }
  },

  data: {
    formats: {},
    readOnly: false,
    placeholder: '开始输入...',
    editorHeight: 300,
    keyboardHeight: 0,
    isIOS: false
  },
   
  ready() {
    this.onEditorReady()
  },
  methods: {
    onEditorReady() {
      const that = this
      var attached = '#' + this.data.editorId
      wx.createSelectorQuery().select(attached).context(function(res) {
        console.log(res, that.data.editorId)
        that.editorCtx = res.context
      }).exec()
    },


    blur() {
      this.editorCtx.blur() 
    },
    format(e) {
      let {
        name,
        value
      } = e.target.dataset
      if (!name) return
      // console.log('format', name, value)

      this.editorCtx.format(name, value)

    },
    onStatusChange(e) { 
      const formats = e.detail
      this.setData({
        formats
      })
    },
    insertDivider() {
      this.editorCtx.insertDivider({
        success: function() {
          console.log('insert divider success')
        }
      })
    },
    clear() {
      this.editorCtx.clear({
        success: function(res) {
          console.log("clear success")
        }
      })
    },
    removeFormat() {
      this.editorCtx.removeFormat()
    },
    insertDate() {
      const date = new Date()
      const formatDate = `${date.getFullYear()}/${date.getMonth() + 1}/${date.getDate()}`
      this.editorCtx.insertText({
        text: formatDate
      })
    },
    insertImage() {
      const that = this
      wx.chooseImage({
        count: 100,
        success: function(res) {
          console.log(that.editorCtx)
          that.uploadFile(res.tempFilePaths)
          // that.editorCtx.insertImage({
          //   src: res.tempFilePaths[0],
          //   data: {
          //     id: 'abcd',
          //     role: 'god'
          //   },
          //   width: '80%',
          //   success: function() {
          //     console.log('insert image success')
          //   }
          // })
        }
      })
    },

    // 获取内容
    getEditorContent(e) {
      console.log(e,'ee')
      that.editorCtx.getContents({
        success: function(res) {
          console.log(res.html)
          that.triggerEvent("getEditorContent", res.html);//触发回调
        }
      })
    },    
    //上传图片
    uploadFile: function (pics) {
      var that = this;
      that.setData({ 
        loading: true
      })
      for (var i = 0; i < pics.length; i++) {
        wx.uploadFile({
          url: app.globalData.apiUrl + '/upload/' + that.data.fileName + '/fileUpload', 
          filePath: pics[i],
          name: 'file',
          success: function (res) {
            if (typeof res.data == "string") {
              var resdata = JSON.parse(res.data)
            }
            if (resdata.code == '0000') {
              that.setData({
                loading: false
              }) 
              that.editorCtx.insertImage({
                src: resdata.data.host+ resdata.data.path,
                    // data: {
                    //   id: 'abcd',
                    //   role: 'god'
                    // },
                    // width: '80%',
                    success: function() {
                      console.log('insert image success')
                    }
                  })
            }
          },
          fail: function (res) {
            that.setData({
              loading: false
            }) 
            wx.showToast({
              icon: 'none',
              title: '图片上传失败',
            })
          }

        })
      }
    },





  }


})